package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
    库管类
        使用集合来作为数据仓库
 */
public class OtherStudentDao implements BaseStudentDao {

    //1. 定义一个长度为5的数组,作为整个系统的数据容器
    private static ArrayList<Student> stus = new ArrayList<Student>();

    //静态代码块: 在系统进行类加载时读取文件数据进行信息的初始化
    static {
        System.out.println("---------------loading--------------------");
        //load01();
        load02();
        ;
    }


    //库管类中的添加功能
    public boolean addStudent(Student stu) {
        stus.add(stu);

        //数据存档
//        reSave01();
        reSave02();
        return true;
    }


    public boolean addStudent2(Student stu) {
        boolean add = stus.add(stu);
        //reSave04();
        return add;
    }


    //库管类中的查询功能
    public Student[] findAllStudents() {
        //将集合转为数组
        //1. 定义一个数组,长度和集合的长度保持一致
        Student[] res = new Student[stus.size()];

        //2. 对集合容器中的数据进行排序
      /*  Collections.sort(stus, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return  o1.getSid().compareTo(o2.getSid());
            }
        });*/


        stus.toArray(res);

        //3. 返回数组
        return res;
    }


    public Student[] findAllStudents2() {
        return stus.toArray(new Student[stus.size()]);
    }

    //库管类中的删除功能
    public void deleteStudentId(String delId) {
        //1. 找到当前要删除的学号对应的学生对象在数组容器中索引位置
        int index = getIndex(delId);
        //2. 用null去覆盖掉该索引位置的学生对象,从而达到删除效果
        stus.remove(index);
        //数据存档
        //reSave01();
        reSave02();

    }


    public void deleteStudentId2(String delId) {
        int index = getIndex(delId);
        stus.remove(index);
    }


    //库管类中的修改功能
    /*public void updateStudentById(String updateId, Student newStu) {
        //1. 通过要修改的学号查找对应学生对象在数组容器中的索引位置
        int index = getIndex(updateId);
        //2. 将newStu去覆盖掉该索引位置的旧对象,从而达到修改效果
        stus.set(index, newStu);
        //数据存档
        //reSave01();
        reSave02();

    }*/

    //第三次修改代码
    @Override
    public void updateStudentById(String updateId, Student newStu) {
        //获取指定学号对象的索引
        int index = getIndex(updateId);
        //修改指定索引位置的对象
        stus.set(index, newStu);
        //修改后进行数据存档
        reSave01();

    }


    public void updateStudentById2(String updateId, Student newStu) {
        int index = getIndex(updateId);
        stus.add(index, newStu);
    }


    //根据学号查找对应学生对象在数组容器中的索引位置
    public int getIndex(String id) {
        //1. 先假设要查找的学号不存在,index先记录-1
        int index = -1;
        //2. 遍历数组容器
        for (int i = 0; i < stus.size(); i++) {
            //2.1 拿到每一个学生对象
            Student stu = stus.get(i);
            //2.2 判断当前学生对象的学号和要查找的学号是否相同
            if (stu != null && stu.getSid().equals(id)) {
                //相同,则说明要查找的学号存在,则修改标记为当前学生对象的索引
                index = i;
                break;
            }
        }

        //3. 返回index即可
        return index;
    }


    public int getIndex11(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            if (stus.get(i) != null & stus.get(i).getSid().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }


    public int getIndex2(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            if (stus.get(i) != null && stus.get(i).getSid().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public int getIndex3(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            if (stus.get(i) != null && stus.get(i).equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }


    //将系统中最新的数据写出到文件,进行存档   --  基于普通字节输出流
    public void reSave01() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("黑马学生管理系统\\stu.txt");
            for (Student stu : stus) {
                fos.write(stu.toString().getBytes());
                fos.write(System.lineSeparator().getBytes());
            }
        } catch (Exception e) {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }


    public void reSave011() {
        try {
            FileOutputStream fos = new FileOutputStream("黑马学生管理系统\\stu.txt");
            for (Student student : stus) {
                fos.write(student.toString().getBytes());
                fos.write(System.lineSeparator().getBytes());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //将上次存档的数据重新读回内存   --  基于普通字节输入流
    public static void load01() {
        try {
            FileInputStream fis = new FileInputStream("黑马学生管理系统\\stu.txt");
            byte[] bys = new byte[1024 * 1024];
            int len = fis.read(bys);
            String s = new String(bys, 0, len);
            String[] ss = s.split(System.lineSeparator());
            for (String s1 : ss) {
                String[] split = s1.split(",");
                stus.add(new Student(split[0], split[1], split[2], split[3]));
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    public static void load011() {
        try {
            FileInputStream fis = new FileInputStream("黑马学生管理系统\\stu.txt");
            byte[] bys = new byte[1024 * 1024];
            int len = fis.read(bys);
            String s = new String(bys, 0, len);
            String[] strings = s.split(System.lineSeparator());
            for (String string : strings) {
                String[] split = string.split(",");
                stus.add(new Student(split[0], split[1], split[2], split[3]));
            }


        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    public static void load02() {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("黑马学生管理系统\\stu.txt"));
            byte[] bys = new byte[1024 * 1024];
            int len = bis.read(bys);
            String studnets = new String(bys, 0, len);
            String[] split = studnets.split(System.lineSeparator());
            for (String s : split) {
                String[] split1 = s.split(",");
                Student student = new Student(split1[0], split1[1], split1[2], split1[3]);
                stus.add(student);
            }

        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    public static void load021() {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("黑马学生管理系统\\stu.txt"));
            byte[] bys = new byte[1024 * 1024];
            int len = bis.read(bys);
            String s = new String(bys, 0, len);
            String[] strings = s.split(System.lineSeparator());
            for (String string : strings) {
                String[] split = string.split(",");
                stus.add(new Student(split[0], split[1], split[2], split[3]));
            }

        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    public static void load3() {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("黑马学生管理系统\\stu.txt"));

            byte[] bys = new byte[1024 * 1024];
            int len = bis.read(bys);
            String s = new String(bys, 0, len);
            String[] ss = s.split(System.lineSeparator());
            for (String s1 : ss) {
                String[] split = s1.split(",");
                stus.add(new Student(split[0], split[1], split[2], split[3]));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void reSave02() {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("黑马学生管理系统\\stu.txt"));
            for (Student student : stus) {
                bos.write(student.toString().getBytes());
                bos.write(System.lineSeparator().getBytes());
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void reSave021() {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("黑马学生管理系统\\stu.txt"));
            for (Student student : stus) {
                bos.write(student.toString().getBytes());
                bos.write(System.lineSeparator().getBytes());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void reSave03() {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("黑马学生管理系统\\stu.txt"));
            for (Student student : stus) {
                bos.write(student.toString().getBytes());
                bos.write(System.lineSeparator().getBytes());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
